<?php

namespace app\teacher\controller\v1;

use app\common\controller\BaseController;
use app\teacher\validate\ShareFileValidate;
use app\teacher\validate\UploadFileValidate;
use think\Controller;
use think\Request;
use app\teacher\model\TeacherFile as TeacherFileModel;
use app\teacher\model\User as UserModel;
use app\teacher\model\UserFile as UserFileModel;

class File extends BaseController
{
    /**
     * 教师课件
     */
    public function index()
    {
        $size = $this->request->get('size');
        $files = TeacherFileModel::getByTeacherId($this->teacherId, $size);
        $files->total() ? $this->success('', [
            'files' => $files->items(),
            'total' => $files->total()
        ]) : $this->notFoundData();
    }

    /**
     * 上传课件
     */
    public function upload(UploadFileValidate $uploadFileValidate, TeacherFileModel $teacherFileModel)
    {
        $this->isPost();
        $data = $this->request->post();
        $this->paramValidate($uploadFileValidate, $data);
        $data['teacher_id'] = $this->teacherId;
        $res = $teacherFileModel->add($data);
        $res ? $this->success('', $teacherFileModel->teacher_file_id) : $this->forbidden();
    }

    /**
     * 学生
     */
    public function student()
    {
        $users = UserModel::getStudentByTeacher($this->teacherId);
        $this->success('', compact('users'));
    }

    /**
     * 分享
     */
    public function share(ShareFileValidate $shareFileValidate)
    {
        $this->isPost();
        $data = $this->request->post();
        $this->paramValidate($shareFileValidate, $data);
        $res = UserFileModel::add($data['user_id'], $data['teacher_file_id']);
        $res ? $this->success() : $this->forbidden();
    }

    /**
     * 删除课件
     */
    public function delete()
    {
        $fileId = $this->notEmpty('teacher_file_id');
        $file = TeacherFileModel::find($fileId);
        if (!$file) {
            $this->errorMsg(20003);
        }
        $res = $file->delete();
        $res ? $this->success() : $this->forbidden();
    }
}
